<?php

/**
 * 新闻
 */
namespace app\api\controller\goods;

use app\api\controller\Common;
class News extends Common
{
    protected $data;
    public function initialize(){
        parent::initialize();
        $this->data = $this->request->except(['sign','accessKey','token']);
    }

    public function lists(){
        $page = $this->data['page'] ? $this->data['page'] : 1;
        $pageSize = $this->data['limit'] ? $this->data['limit'] : 20;
        $paginate = ['list_rows'=>$pageSize,'page'=>$page];
        $category =$this->data['category'];
        $name = $this->data['name'];
        $sort_data = $this->data['sort'];
        $where[] = ['g.del','=',0];
        if($category){
            $where[] = ['g.category','=',$category];
        }
        if($name){
            $where[] = ['g.title','like','%'.$name.'%'];
        }
        // 排序方式
        if($sort_data){
            $sort_arr = [1=>'hit',2=>'create_time'];
            $sort = $sort_arr[$sort_data];
        }else{
            $sort = 'hit';
        }
        $list = db('news')->alias('g')
            ->field('g.id,g.title,g.pic,g.create_time,gc.title as category_name')
            ->join('goods_category gc','gc.id = g.category')
            ->where($where)->order($sort,'desc')->paginate($paginate)->toArray();
        if($list){
            if($list['data']){
                foreach ($list['data'] as $key=>$val){
                    $time_format = $this->format_date($val['create_time']);
                    $list['data'][$key]['format_time'] = $time_format;
                    $uid = $this->user['id'];
                    $is_collect = "";
                    if($uid){
                        $is_collect = db('news_collect')->where(['did'=>$val['id'],'uid'=>$uid])->find();
                    }
                    $list['data'][$key]['is_collect'] = $is_collect ? 1 : 0;
                }
            }
            $return = [
                'page' => $page,
                'pageNum' => $list['current_page'],
                'limit' => $pageSize,
                'total' => $list['total'],
                'list' => $list['data']
            ];
            $this->return = ['code'=>'SUCCESS','status'=>200,'message'=>'成功','data'=>$return];
        }else{
            $this->return = ['code'=>'GET_ERROR','status'=>200,'message'=>'无数据'];
        }
        return $this->show($this->return);
    }

    public function detail(){
        $id = input('id');
        $detail = db('news')->field('id,title,pic,content,create_time')->where('id',$id)->find();
        if($detail){
            // 是否收藏
            $uid = $this->user['id'];
            $is_collect = "";
            if($uid){
                $is_collect = db('news_collect')->where(['did'=>$id,'uid'=>$uid])->find();
            }
            $detail['is_collect'] = $is_collect ? 1 : 0;
            db('news')->where('id',$id)->setInc('hit'); // 浏览量+1
            $detail['format_data'] = $this->format_date($detail['create_time']);
            $this->return = ['code'=>'SUCCESS','status'=>200,'message'=>'成功','data'=>$detail];
        }else{
            $this->return = ['code'=>'GET_ERROR','status'=>200,'message'=>'无数据'];
        }
        return $this->show($this->return);
    }

    /**
     * 相关推荐
     */
    public function more(){
        $id = input('id');
        $category = db('news')->where('id',$id)->value('category');
        if($category){
            $res = db('news')->field('id,title,pic')->where('category',$category)->order('hit','desc')->limit(4)->select();
            $this->return = ['code'=>'SUCCESS','status'=>200,'message'=>'成功','data'=>$res];
        }else{
            $this->return = ['code'=>'GET_ERROR','status'=>200,'message'=>'无数据'];
        }
        return $this->show($this->return);
    }

    /**
     * 收藏/取消
     */
    public function newsCollect(){
        $this->checkUserLogin(); // 验证登录
        $data = $this->data;
        $uid = $this->user['id'];
        $data['uid'] = $uid;
        $data['create_time'] = date("Y-m-d H:i:s",time());
        $is_collect = db('news_collect')->where(['did'=>$data['did'],'uid'=>$uid])->find();
        if($is_collect){
            $res = db('news_collect')->where(['did'=>$data['did'],'uid'=>$uid])->delete();
            if($res){
                $this->return = ['status'=>200,'code'=>'SUCCESS','message'=>'成功','data'=>2];
            }else{
                $this->return = ['status'=>200,'code'=>'GET_ERROR','message'=>'失败','data'=>4];
            }
        }else{
            $res = db('news_collect')->insert($data);
            if($res){
                $this->return = ['status'=>200,'code'=>'SUCCESS','message'=>'成功','data'=>1];
            }else{
                $this->return = ['status'=>200,'code'=>'GET_ERROR','message'=>'失败','data'=>3];
            }
        }
        return $this->show($this->return);
    }

    /**
     * 7.6 我收藏的新闻列表
     */
    public function myCollect(){
        $this->checkUserLogin();
        $uid = $this->user['id'];
        $page = $this->data['page'] ? $this->data['page'] : 1;
        $pageSize = $this->data['limit'] ? $this->data['limit'] : 20;
        $paginate = ['list_rows'=>$pageSize,'page'=>$page];
        $res = db('news_collect')->alias('a')
            ->field('b.id,b.title,b.pic,b.hit')
            ->join('news b', 'a.did = b.id')
            ->where(['a.uid'=>$uid])->order('a.id DESC')->paginate($paginate)->toArray();
       
        return $this->showSuccess('我收藏的新闻列表',$res);
    }

}